//
//  SavourCollection.m
//  Lovelife
//
//  Created by dllo on 15/4/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SavourCollection.h"
#import "CellForCollect.h"
#import "UIImageView+WebCache.h"
#import "ItemZeroModel.h"
#import "WebViewController.h"

@interface SavourCollection ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableview;
@property (nonatomic,retain)NSMutableArray *modelArr;

@end

@implementation SavourCollection

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
        
        self.modelArr = [NSMutableArray array];
    }
    return self;

}

-(void)dealloc
{
    [_tableview release];
    [super dealloc];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableview = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    
    self.tableview.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableview];
    
    [_tableview release];
    
    self.tableview.delegate = self;
    
    self.tableview.dataSource = self;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return self.view.frame.size.height/6;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *sandPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *sandBoxPath = [sandPath lastObject];
    
    NSString *storePath = [sandBoxPath stringByAppendingPathComponent:@"itemzero.plist"];
    
    self.modelArr = [NSKeyedUnarchiver unarchiveObjectWithFile:storePath];
    
    NSLog(@"%ld",self.modelArr.count);
    
    return self.modelArr.count;
    
    
    

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    
    CellForCollect *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    
    if (!cell) {
        
        cell = [[[CellForCollect alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]autorelease];
        
        
    }
    
    ItemZeroModel *zero = self.modelArr[indexPath.row];
    [cell.Img sd_setImageWithURL:[NSURL URLWithString:zero.thumb]];
    
    cell.title.text  = zero.title;
    
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WebViewController *web = [[WebViewController alloc]init];
    
    ItemZeroModel *zero = self.modelArr[indexPath.row];
    NSLog(@"%@",zero.url);

    web.url = zero.url;
    
    web.model = zero;
    [self.navigationController pushViewController:web animated:YES];
    
    [web release];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
